include('shared.lua')

--this is all a bit of a GMDM rip.

local matBeam = Material ("effects/tool_tracer")
local matStart = Material( "Sprites/light_glow02_add" )
local matEnd = Material( "Sprites/light_glow02_add_noz" )

function ENT:Draw ()
	local Owner = self.Entity:GetOwner()
	if not IsValid(Owner) then return end
	
	local startPos		= self.Entity:GetPos()
	local endPos		= self.Entity:GetEndPos() or Vector (0,0,0)
	
	local trace		= {}
	
	local vm = Owner:GetActiveWeapon()
	local iZ = false
	
	if LocalPlayer() == Owner then
		vm = Owner:GetViewModel()
		iZ = true
	end
	if not IsValid (vm) then return end
	local attachment = vm:GetAttachment (1)
	startPos = attachment.Pos - ( attachment.Ang:Up() * 2 )
	self:SetPos( startPos )
		
	trace.start = Owner:EyePos()
	
	trace.endpos = trace.start + (Owner:EyeAngles():Forward() * 192)
	trace.filter = {Owner, Owner:GetActiveWeapon()}
	
	local tr = util.TraceLine (trace)
	
	endPos = tr.HitPos
	self:SetEndPos( endPos )
	
	--draw the damn beam (THIS is a rip of ToolTracer)
	--render.IgnoreZ (iZ)
	
	local length = (startPos - endPos):Length()
	
	render.SetMaterial(matStart)
	render.DrawSprite( startPos, 16, 16, Color( 0, 255, 0, 255 ) )
	
	render.SetMaterial(matEnd)
	render.DrawSprite( endPos, 16 + ( math.random() * 4 ), 16 + ( math.random() * 4 ), Color( 0, 255, 0, 255 ) )
	
	render.SetMaterial (matBeam)
	local texcoord = math.Rand (0, 1)
	
	for i=1, 4 do
		render.DrawBeam(startPos, 										// Start
					 endPos,											// End
					 8,													// Width
					 texcoord,														// Start tex coord
					 texcoord + length / 128,									// End tex coord
					 Color(0, 255, 0, 255))		// Color (optional)
	end
	
	--render.IgnoreZ (false)
end

